package com.hongkang.ecg;

import java.util.ArrayList;
import java.util.Date;

import com.hongkang.database.EcgRecordInfoType;
import com.hongkang.type.EcgRecordBean;
import com.hongkang.utils.ScopeDataUtils;
import com.hongkang.utils.Utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class EcgRecordActivity extends BaseActivity{
	ListView mRecordList;
	EcgRecordListAdapter mAdapter;
	ArrayList<EcgRecordInfoType> mEcgRecordList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ecg_record_list);
		mRecordList = (ListView) findViewById(R.id.ecg_record_list);
		
		//add test data to record list
		mEcgRecordList = ScopeDataUtils.getAllEcgRecordList();
		mAdapter = new EcgRecordListAdapter();
		mRecordList.setAdapter(mAdapter);
		mRecordList.setOnItemClickListener(mEcgRecordListItemClickListener);
	}
	
	private class EcgRecordListitemLayout extends LinearLayout{
		public TextView mUserName;
		public TextView mTestTime;
		//public TextView mTestResult;
		public EcgRecordListitemLayout(Context context) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.ecg_record_list_item, this);
			mUserName = (TextView) this.findViewById(R.id.scope_record_id);
			mTestTime = (TextView) this.findViewById(R.id.test_time);
			//mTestResult = (TextView) this.findViewById(R.id.test_result);
		}
	}
	
	private class EcgRecordListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mEcgRecordList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mEcgRecordList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			EcgRecordListitemLayout view;
			if(convertView != null){
				view = (EcgRecordListitemLayout) convertView;
			}
			else{
				view= new EcgRecordListitemLayout(EcgRecordActivity.this);
			}
			EcgRecordInfoType infoType = (EcgRecordInfoType) getItem(position);
			view.mUserName.setText(infoType.mId);
			view.mTestTime.setText(new Date( infoType.mCreateTime).toLocaleString());
//			view.mUserName.setText(mRecords.get(position).user_name);
//			view.mTestTime.setText(mRecords.get(position).test_time);
//			view.mTestResult.setText(mRecords.get(position).test_result);
//			if(position == 1){
//				view.mTestResult.setTextColor(Color.parseColor("#FF0000"));
//			}
			return view;
		}
	}
	
	//click a item
	protected OnItemClickListener mEcgRecordListItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			Utils.viewStaticEcgScopeActivity(EcgRecordActivity.this, mEcgRecordList.get(position));
		}
	};
}
